<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */

global $framework;
require_once("$framework/debug.php");
require_once("$framework/session.php");
require_once("$framework/wizard/wizard.php");


class MultiPageWizard extends Wizard{
	protected $_maxPage=0;
	protected $_pages = array();
	
	public function __construct($parent,$method){
		parent::__construct($parent,$method);
	}
	
	public function setPage($page){
		__debug(__CLASS__."::setPage() - start : page=$page",__FILE__,__LINE__);
		
		if($page <= $this->_maxPage){
			__debug(__CLASS__."setPage() - set page=$page",__FILE__,__LINE__);
			$this->setActive($page);
		}else{
			__error(__CLASS__."setPage() - Can't set as active a page superior to ".$this->_maxPage,__FILE__,__LINE__);
		}
		__debug(__CLASS__."::setPage() - end",__FILE__,__LINE__);
	}
	
	public function getPage(){
		$page=$this->getActive();
		if($page=="unknown"){
			$page="0";
		}
		__debug(__CLASS__."::getPage() - return=$page",__FILE__,__LINE__);
		return $page;
	}

	/**
	 * calculate next page number.
	 * @return String containing the next page number
	 */
	public function nextPage(){
		__debug(__CLASS__."::nextPage() - start",__FILE__,__LINE__);
		$page=0;
		if($this->getPage()+1 < $this->_maxPage){
			$page=$this->getPage()+1;
		}else{
			$page=$this->getPage();
		}
		$this->setPage($page);
		__debug(__CLASS__."::nextPage() - return=$page",__FILE__,__LINE__);
		return $page;
	}
	/**
	 * calculate previous page number.
	 * @return String containing the previous page number
	 */
	public function previousPage(){
		__debug(__CLASS__."::previousPage() - start",__FILE__,__LINE__);
		$page=0;
		if((($this->getPage()-1)) >= 0){
			$page=$this->getPage()-1;
		}else{
			$page=$this->getPage();
		}
		$this->setPage($page);
		__debug(__CLASS__."::previousPage() - return=$page",__FILE__,__LINE__);
		return $page;
	}

	/**
	 * Retrieve active page from session or set it to a default value.
	 * @param $default default page to be displayed.
	 * @return string
	 */	
	public function retrievePage($default){
		$page=$this->getPage();
		if($page=="unknown"){
			$this->setPage($default);
		}
		return $this->getPage();
	}
	
	/**
	 * return the composed name of the theme's template for this step of the wizard.
	 * @return unknown_type
	 */
	public function getTemplate(){
		$template = $this->_parent->getName()."/".$this->_method."_".$this->getPage();
		return $template;
	}

	/**
	 * Process the corresponding action from user action on Wizard.
	 * @param $type
	 * @param $id
	 * @return unknown_type
	 */
	public function action($type,$id){
		switch($type){
			case "next":
				$this->nextPage();
				break;
			case "previous":
				$this->previousPage();
				break;
		}
	}

	/**
	 * Draw actions
	 * @return unknown_type
	 */
	public function getLinkedActions(){
		$actions = "<p>";
		if($this->getPage()!=0){
		$actions .= Html::link("cart","pay","previous","page",__("payment_wizard","previous_title"),
			array(	'description'=>__("payment_wizard","previous_description"),
					'accesskey'=>__("payment_wizard","next_accesskey"),
					'styleclass'=>"smallbutton fright",
					'image'=>"webapp/images/icons/control_rewind.png"));
		}
		if($this->getpage()!=$this->_maxPage){
			$actions .= Html::link("cart","pay","next","page",__("payment_wizard","next_title"),
				array(	'description'=>__("payment_wizard","next_description"),
						'accesskey'=>__("payment_wizard","next_accesskey"),
						'styleclass'=>"smallbutton fright",
						'image'=>"webapp/images/icons/control_fastforward.png"));
		}
		$actions .="</p>";
		return $actions;		
	}

}
?>